package com.itheima.controller;

import com.itheima.controller.utils.R;
import com.itheima.controller.utils.Yzm;
import com.itheima.domain.Person;
import com.itheima.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;

@RestController
@RequestMapping("/student")



public class LoginControl {
    @Autowired
    private LoginService loginService;

//    String s="a" ;
//    @GetMapping("/log/{c}" )
//    public String loginin( @PathVariable Integer c) throws IOException {
//        System.out.println(c);
//
//        File file = new File("src/main/resources/static/yzm"+c+".jpg");
//         s = Yzm.CaptchaUtil.generateVerifyCode(4);
//
//
//
//
//        System.out.println(s);
//        Yzm.CaptchaUtil.outputVerifyImage(250, 100, file, 4);
//        return "/pages/login.html";
//    }


    @PostMapping("/login2")
    public R login2(@RequestBody Person person, HttpSession session) throws IOException {
        System.out.println(person);
//        if ("".equals(yzm)||"null".equals(yzm)){
////            return new R(false,"验证码为空");
//            return "redirect:/pages/login.html";
//        }
//
//        if (s.equals(yzm)||s.equals("a")){
////            return new R(false,"验证码错误");
//
//        }else {
//            return "redirect:/pages/login.html";
//        }


        if ("".equals(person.getUsername())||"null".equals(person.getUsername())) {
            return new R(false,"账号为空");
//            return "redirect:/pages/login.html";
        }
        if ("".equals(person.getPassword())||"null".equals(person.getPassword())) {
            return new R(false,"密码为空");
//            return "redirect:/pages/login.html";
        }
        boolean b = loginService.b(person.getUsername(), person.getPassword());

        if (b){
//            return "redirect:/pages/student.html";
            return new R(true,"登录成功");
        }

        return new R(false,"密码错误");
    }
}
